package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserAdministrationService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.basic.BasicPasswordFieldUI;
import java.util.Arrays;
import java.util.List;

//用户管理

@RequestMapping("/useradministration")
@RestController
public class UserAdministrationController {

    @Reference
    private UserAdministrationService userAdministrationService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //分页查询   查询所有用户信息
    @PostMapping("/findPage")
    public PageResult administration(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = null;
        try {
            pageResult = userAdministrationService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    //查询回显所有角色信息
    @GetMapping("/findAll")
    public Result findAll() {

        try {
            List<Role> list = userAdministrationService.findAll();
            return new Result(true, "查询所有角色信息成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有角色信息失败");
        }
    }

    //添加用户 或者 修改用户
    @PostMapping("/add")
    public Result add(@RequestBody User user, Integer[] checkgroupIds) {
        //判断用户名是否为空
        if (user.getUsername() == null || "".equals(user.getUsername())) {
            return new Result(false, "用户名不能为空,请重新输入");
        }
        if (user.getPassword() == null || "".equals(user.getPassword())) {
            return new Result(false, "密码不能为空,请重新输入");
        }
        if (user.getTelephone() == null || "".equals(user.getTelephone())) {
            return new Result(false, "密码不能为空,请重新输入");
        }
        Result result = null;
        //密码通过盐加密
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        try {
            result = userAdministrationService.add(user, checkgroupIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    //删除用户
    @PostMapping("/deleteUser")
    public Result deleteUser(Integer id) {
        Result result = null;
        try {
            result = userAdministrationService.deleteUser(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    //根据id查询回显对应用户所有信息
    @GetMapping("/echoCompile")
    public Result echoCompile(Integer id) {

        try {
            User user = userAdministrationService.echoCompile(id);
            return new Result(true, "查询用户信息回显成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户信息回显失败");
        }
    }

    //根据用户id查询对应角色进行数据回显
    @GetMapping("/echoRole")
    public Result echoRole(Integer id) {

        try {
            Integer[] integers = userAdministrationService.echoRole(id);
            return new Result(true, "查询角色信息回显成功", integers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息回显失败");
        }
    }
}
